package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.constant.Constants;
import com.itany.pojo.Book;
import com.itany.pojo.ManagerUser;
import com.itany.service.BookService;
import com.itany.vo.ActionResult;
import com.itany.vo.BookVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Author:梁辰
 * 时间:19-4-1 上午9:08
 * description:
 * version:1.0
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;
    @RequestMapping("/insertBook")
    public ActionResult insertBook(Book book , HttpSession session, @RequestParam(value = "file",required = false)MultipartFile file) throws FileNotFoundException {
        ActionResult ar=new ActionResult();
        ManagerUser user=(ManagerUser)session.getAttribute("sessionUser");
        bookService.addBook(book,user,file);
        ar.setStatus(true);
        ar.setMsg("添加成功");
        return ar;
    }
    @RequestMapping("/findByFuzzy")
    public Map<String,Object> findByFuzzy(BookVo vo,@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "5")Integer rows){
        Map<String,Object> map = new HashMap<>();
        PageInfo<BookVo> byFuzzy = bookService.findBookByFuzzy(vo, page, rows);
        map.put("total",byFuzzy.getTotal());
        map.put("rows",byFuzzy.getList());
        return map;
    }

    @RequestMapping("/modifyEnFlagById")
    public ActionResult modifyEnFlagById(Book book){
        ActionResult ar = new ActionResult();
        book.setFlag(Constants.ENABLE_BOOK_FLAG);
        bookService.modifyEnFlagById(book);
        ar.setStatus(true);
        ar.setMsg("启用成功");
        return ar;
    }

    @RequestMapping("/modifyDisFlagById")
    public ActionResult modifyDisFlagById(Book book){
        ActionResult ar = new ActionResult();
        book.setFlag(Constants.DISABLE_BOOK_FLAG);
        bookService.modifyDisFlagById(book);
        ar.setStatus(true);
        ar.setMsg("禁用成功");
        return ar;
    }

    @RequestMapping("/modifyById")
    public ActionResult modifyById(Book book,@RequestParam(value = "file",required = false) MultipartFile file) throws FileNotFoundException {
        ActionResult ar = new ActionResult();
        bookService.modifyById(book,file);
        ar.setStatus(true);
        ar.setMsg("修改成功");
        return ar;
    }
}
